<template>
  <view class="container">
    <!-- Web-view页面 -->
    <web-view v-if="webViewUrl" :src="webViewUrl" @message="handleMessage"></web-view>
  </view>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue';

const webViewUrl = ref('');
const webViewType = ref('');

onMounted(() => {
  // 获取页面参数
  const pages = getCurrentPages();
  const currentPage = pages[pages.length - 1];
  const options = currentPage.options || currentPage.$page.options;
  
  webViewType.value = options.webViewType || '';
  
  // 处理不同类型的web-view
  if (webViewType.value === 'customerService') {
    // 在微信小程序环境中，调用原生API打开客服会话窗口
    if (uni.getSystemInfoSync().platform === 'mp-weixin') {
      const corpid = options.corpid;
      const customerUrl = decodeURIComponent(options.customerUrl);
      
      // 使用微信小程序原生API打开客服会话窗口
      wx.openCustomerServiceChat({
        extInfo: { url: customerUrl },
        corpId: corpid,
        success: () => {
          console.log('客服会话窗口打开成功');
          // 打开成功后返回上一页
          setTimeout(() => {
            uni.navigateBack();
          }, 500);
        },
        fail: (err) => {
          console.error('客服会话窗口打开失败:', err);
          uni.showToast({
            title: '客服连接失败',
            icon: 'none'
          });
          uni.navigateBack();
        }
      });
    } else {
      // 非微信环境下可以直接跳转到URL
      webViewUrl.value = decodeURIComponent(options.customerUrl);
    }
  }
});

// 处理web-view消息
const handleMessage = (event) => {
  console.log('接收到web-view消息:', event);
};
</script>

<style>
.container {
  width: 100%;
  height: 100vh;
}
</style> 